<?php
/**
 *
 * User:xinhuazhang
 * Date:2024/5/15
 * TIPS:功能说明
 * Email:<1655626823@qq.com>
 */

namespace app\api\controller;

use app\admin\model\Designexample;
use app\admin\model\Furnish;
use app\common\controller\Api;
use app\common\model\Brands;

/*获取设计列表 d店铺装修 品牌加盟
 * */

#[Apidoc\Title("获取设计列表 d店铺装修 品牌加盟")]
class Designs extends Api
{

    #[
        Apidoc\Title("获取设计列表 和店铺装修"),
        Apidoc\Tag("address	详细地址
city	地址
company_name	公司名称
create_time	创建时间
establish_time	成立时间
id	ID
images	相册
introduction	公司简介
main_business	主营业务
status	状态:0=禁用,1=启用
team_scale	团队规模
type	design 商业设计 furnish 装饰
update_time	更新时间
weigh	权重"),
        Apidoc\Method("POST"),
        Apidoc\Url("/api/Designs/designsList"),
        Apidoc\Query(name: "type", type: "string", require: true, desc: "类型 design 商业设计 furnish 店铺装修", mock: "design"),
        Apidoc\Query(name: "page", type: "number", require: false, desc: "页码", mock: "1"),
        Apidoc\Query(name: "keyword", type: "sting", require: false, desc: "关键词搜索", mock: ""),
        Apidoc\Returned("id", type: "int", desc: "Id"),
    ]
    public function designsList()
    {
        $type = $this->request->param('type');
        $page = $this->request->param('page', 1);
        $keyword = $this->request->param('keyword');
        $where = [
            ['type', '=', $type],
            ['status', '=', 1]
        ];
        if ($keyword) {
            $where[] = ['company_name', 'like', "%$keyword%"];
        }
        $model = new Furnish();
        $model2 = new Designexample();
        $list = $model
            ->where($where)
            ->order('weigh', ' desc')
            ->paginate()->each(function ($item) use ($model2) {
                $exam_list =$images=$model2->order('id', 'desc')
                    ->limit(3)
                    ->where('furnish_id', $item['id'])
                    ->column('image');
                foreach ($exam_list as $k => &$v) {
                    $v = request()->domain() . $v;
                }
                $item['domain_images'] =$exam_list;
                $item['images'] =$images;
            });
        $this->success('获取成功', $list);
    }

    #[
        Apidoc\Title("获取设计列表 共用 店铺装修  详情"),
        Apidoc\Tag("apidoc,基础,示例"),
        Apidoc\Method("POST"),
        Apidoc\Url("/api/Designs/designsDetail"),
        Apidoc\Query(name: "id", type: "number", require: true, desc: "页码", mock: "1"),
        Apidoc\Returned("examples", type: "array", desc: "设计案列"),
        Apidoc\Returned("establish_time_text", type: "string", desc: "成立时间（只有年月日）"),
    ]
    public function designsDetail()
    {
        $id = $this->request->param('id');
        $model = new Furnish();
        $info = $model->with(['examples', 'user'])
            ->where(['id' => $id, 'status' => 1])
            ->find();
        if (!$info) {
            $this->error('数据不存在');
        }
        $info->visible(['user' => ['id', 'nickname', 'avatar', 'domain_avatar','mobile']]);
        $result = $info->toArray();
        $model2 = new Designexample();
        $exam_list = $model2->order('id', 'desc')
            ->where('furnish_id', $info['id'])
            ->column('image');
        foreach ($exam_list as $k => &$v) {
            $v = request()->domain() . $v;
        }
        $result['domain_images'] = $exam_list;
        $result['establish_time'] = $result['establish_time_text'];
        //$info->visible(['user'=>'id,nickname,avatar,domain_avatar,gender']);
        $this->success('获取成功', $result);
    }

    #[
        Apidoc\Title("案列  详情"),
        Apidoc\Tag("apidoc,基础,示例"),
        Apidoc\Method("POST"),
        Apidoc\Url("/api/Designs/exampleDetail"),
        Apidoc\Query(name: "id", type: "number", require: false, desc: "页码", mock: "1"),
        Apidoc\Returned("content", type: "string", desc: "富文本显示"),
    ]
    public function exampleDetail()
    {
        $id = $this->request->param('id');
        $model = new Designexample();
        $info = $model
            ->where(['id' => $id, 'status' => 1])
            ->find();
        if (!$info) {
            $this->error('数据不存在');
        }
        $this->success('获取成功', $info);
    }

    #[
        Apidoc\Title("品牌加盟列表"),
        Apidoc\Tag("apidoc,基础,示例"),
        Apidoc\Method("POST"),
        Apidoc\Url("/api/Designs/joinBrand"),
        Apidoc\Param(name: "page", type: "number", require: false, desc: "页码", mock: "1"),
        Apidoc\Param(name: "keyword", type: "sting", require: false, desc: "关键词", mock: ""),
        Apidoc\Returned("content", type: "string", desc: "富文本显示"),
    ]
    public function joinBrand()
    {
        $keyword = $this->request->param('keyword');
        $where = [];
        if ($keyword) {
            $where[] = ['company_name', 'like', "%$keyword%"];
        }
        $model = new Brands();
        $list = $model->where(['status' => 1])->where($where)->paginate();
        $this->success('获取成功', $list);
    }

    #[
        Apidoc\Title("品牌加盟详情"),
        Apidoc\Tag("apidoc,基础,示例"),
        Apidoc\Method("POST"),
        Apidoc\Url("/api/Designs/brandDetail"),
        Apidoc\Query(name: "id", type: "number", require: true, desc: "", mock: "1"),
        Apidoc\Returned("content", type: "string", desc: "富文本显示"),
    ]
    public function brandDetail()
    {
        $model = new Brands();
        $id = $this->request->param('id');
        $list = $model->with(['user'])->where(['status' => 1, 'id' => $id])->find();

        //$list->visible(['user'=>'id,nickname,avatar,domain_avatar,gender']);
        $this->success('获取成功', $list);
    }
}
